package com.vitechinc.v3.dates;

import java.util.Date;
import java.util.List;


public class OverlapUtil {
	
	private OverlapUtil(){
		
	}
	
	/**
	 * Periods are considering overlapping if a single date can be considered to 
	 * fall into two periods. 
	 * @see com.vitechinc.v3.dates.Period
	 * @param periods the list of periods to consider
	 * @return true if the list of periods contains overlapping date periods
	 */
	public static final boolean containsOverlap(List periods)
	{
		Date periodStart;
		Date periodEnd;
		Date periodStart2;
		Date periodEnd2;
		
		for(int i=0;i<periods.size();i++)
		{
			periodStart = ((Period)periods.get(i)).getStartDate();
			periodEnd = ((Period)periods.get(i)).getStopDate();
			for(int j=0;j<periods.size();j++)
			{
				if(i!=j)
				{
					periodStart2 = ((Period)periods.get(j)).getStartDate();
					periodEnd2 = ((Period)periods.get(j)).getStopDate();
					if((periodStart.before(periodEnd2) || periodStart.equals(periodEnd2) ) && (periodStart2.before(periodEnd)|| periodStart.equals(periodEnd2)))
					{
						return true;
					}
				}
			}
			return false;			
		}
		
		throw new UnsupportedOperationException();
	}
}
